<?php

namespace App\Http\Controllers;

use App\Theme;
use App\Writing;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest');
    }
    public function index(Request $request){

        return view('web.user.index');

    }
    //上传作品
    public function upload_writing(){
        return view('web.user.uploadWriting');
    }
    //得到主题
    public function get_theme(Request $request){
        $category_id=$request->category_id;
        $theme=Theme::where('category_id',$category_id)->get();
        return view('web.user.get_theme',compact('theme'));

    }
    //提交作品
    public function writing_store(Request $request){
       // dd($request->all());
        $this->validate($request,[
            'name'=>'required',
            'desc'=>'required',
            'category_id'=>'required',
            'theme_id'=>'required',

        ],[
            'name.required'=>'请填写作品标题',
        ]);
        $writing=new Writing();
        $writing->name=$request->name;
        $writing->desc=$request->desc;
        $writing->category_id=$request->category_id;
        $writing->theme_id=$request->theme_id;
        $writing->thumb_img=$request->thumb_img;
        $writing->master=[$request->master1,$request->master2,$request->master3];
        $writing->video_url=$request->video_url;
        $writing->video=$request->video;
        $writing->author_id=Auth::user()->id;
        $writing->save();
        return redirect()->back()->with(
        ['status'=>'上传成功']
        );
    }
    //作品列表
    public function writing_list(){
        return view('web.user.writing_list');
    }
    //得到更多作品
    public function get_more_writing(Request $request){
        $config=config('admin.writing');
        $list= Writing::with('theme')->where('is_display',1)->paginate(12);
        return view('web.user.get_more_writing',compact('list','config'));
    }
    //作品详情
    public function writing_detail(Request $request){
        $id=$request->id;
        $detail=Writing::with('theme')->where('is_display',1)->findOrFail($id);
        if(!$detail){
            abort(404);
        }
        return view('web.user.writing_detail',compact('detail'));
    }
}
